package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.event.EventPageChange;
import ru.rchervot.sanasar.client.event.EventResultsItem;
import ru.rchervot.sanasar.client.event.EventSelectedNone;
import ru.rchervot.sanasar.client.event.EventSelectedSeveral;
import ru.rchervot.sanasar.client.event.EventUIResultsScrollChange;
import ru.rchervot.sanasar.client.event.EventUIResultsSplitChange;
import ru.rchervot.sanasar.client.event.EventUISectionsWidthChange;
import ru.rchervot.sanasar.client.event.HandlerPageChange;
import ru.rchervot.sanasar.client.event.HandlerResultsItem;
import ru.rchervot.sanasar.client.event.HandlerSelectedNone;
import ru.rchervot.sanasar.client.event.HandlerSelectedSeveral;
import ru.rchervot.sanasar.client.event.HandlerUIResultsScrollChange;
import ru.rchervot.sanasar.client.event.HandlerUIResultsSplitChange;
import ru.rchervot.sanasar.client.event.HandlerUISectionsWidthChange;
import ru.rchervot.sanasar.client.model.FactoryQuery;
import ru.rchervot.sanasar.client.model.ItemObject;
import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;

import com.google.gwt.event.shared.HandlerManager;

public class ContentSearchResultsPresenter implements Presenter {
	
	public interface Display extends Presenter.Display {
		public void changeWidth(int sectionWidth);
		public void changeSplitPosition(int splitPosition);

		public void renderStartPage();
		public void renderSelectedSeveral();
		public void renderRecord();
		public void renderSelectedNone();
		public void scrollToRowOffsetHeight(int rowOffsetHeight);
		public void scrollRecordToTop();
	}
	
	protected final HandlerManager eventBus;
	protected final Display display;
	protected final RPCServiceAsync service;
	protected final FactoryQuery factoryQuery;
	
	ContentSearchResultsPresenter(Display display, HandlerManager eventBus,
			RPCServiceAsync service, 
			FactoryQuery factoryQuery) {
		this.display = display;
		this.eventBus = eventBus;
		this.service = service;
		this.factoryQuery = factoryQuery;
		
		listenUISectionsWidthChange();
		listenUIResultsSplitChange();
		listenUIResultsScrollChange();
		
		listenForSelectedNone();
		listenForSelectedSeveral();
		listenForPageChange();
		listenForItemChange();
	}

	@Override
	public void bindDisplay() {
		display.setPresenter(this);
	}
	
	private void listenUISectionsWidthChange() {
		eventBus.addHandler(EventUISectionsWidthChange.getType(),
				new HandlerUISectionsWidthChange() {
			@Override
			public void onWidthChange(EventUISectionsWidthChange event) {
				display.changeWidth(event.getSectionsWidth());
			}
		});
	}
	
	protected void listenUIResultsSplitChange() {
		eventBus.addHandler(EventUIResultsSplitChange.getType(),
				new HandlerUIResultsSplitChange() {
			@Override
			public void onSplitChange(EventUIResultsSplitChange event) {
				display.changeSplitPosition(event.getSplitPosition());
			}
		});
	}
	
	protected void listenUIResultsScrollChange() {
		eventBus.addHandler(EventUIResultsScrollChange.getType(),
				new HandlerUIResultsScrollChange() {
			@Override
			public void onScrollChange(EventUIResultsScrollChange event) {
				display.scrollToRowOffsetHeight(event.getScrollOffset());
			}
		});
	}
	
	protected void listenForPageChange() {
		eventBus.addHandler(EventPageChange.getType(),
				new HandlerPageChange<SearchResultsItem>() {
			@Override
			public void onPageChange(
					EventPageChange<SearchResultsItem> event) {
				if (event.isStartPage()) {
					display.renderStartPage();
				} else {
					if (event.getPage().isEmpty()) {
						display.renderSelectedNone();
					} else {
						display.renderRecord();
					}					
				}
			}
		});
	}
	
	private void listenForItemChange() {
		eventBus.addHandler(EventResultsItem.getType(),
				new HandlerResultsItem<ItemObject>() {
			@Override
			public void onNewResultsItem(
					EventResultsItem<ItemObject> event) {
				display.scrollRecordToTop();
			}
		});
	}

	protected void listenForSelectedNone() {
		eventBus.addHandler(EventSelectedNone.getType(),
				new HandlerSelectedNone() {
			@Override
			public void onAction(
					EventSelectedNone event) {
				display.renderSelectedNone();
			}
		});
	}
	
	protected void listenForSelectedSeveral() {
		eventBus.addHandler(EventSelectedSeveral.getType(),
				new HandlerSelectedSeveral() {
			@Override
			public void onAction(
					EventSelectedSeveral event) {
				display.renderSelectedSeveral();
			}
		});
	}
}
